<?php
class Upload_form extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('form_model');
		$this->load->model('student_model');
		$this->load->language('class','english');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
		
		$per_page_count 	     = $this->config->item('per_page_count');
		$view 				     = $this->getPath(__FUNCTION__);
		$view['menu_var']  = 2;
		$where                    = array('deleted' => 0);
		$form                     = $this->form_model->getforms($page, $per_page_count , $like, $where );
		$view['form'] 		      = $form->records;
		$view['cur_page'] 	      = $page;
		$view['title']		      = 'Forms List';
		$view['subtitle']	      = 'Forms Lists';
		/************************ Pagination block ***********************************/
		$config['base_url'] 	  = $this->config->item('member_base_url').'/upload_form/index/';
		$config['total_rows'] 	  = $form->total;
		$config['per_page']       = $per_page_count;
		$config['cur_page']       = $page /$per_page_count;
		$config['uri_segment'] 	  = 4;
		$config['num_links'] 	  = 3;
		$this->pagination->initialize($config);
		$view['msg'] 	          = $this->lang->line($this->session->flashdata('form_msg'));
		$this->layout->memberview('form_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		$view['title']           = "form Details";
		$save                    = $this->input->post('save');
		$view['form'] 	         = array();
		$view['cur_page']        = $cur_page;
		$view['subtitle']	     = 'Add form';
		if(!empty($save)) 
		{
			$pdf_file             = $_FILES['file']['name'];
			$ext                  = end(explode('.', $pdf_file));
			$data                 = $this->retrieve->post($this->form_model->fields);
			$insertStatus         = $this->form_model->add($data);
			$view['form']    	  = $data;
		
			//print_r($insertStatus);
			/********************* Image upload************/
			//echo $_FILES['file']['type'];
			if (!($_FILES['file']['type'] =="application/pdf"))
			{
				echo "Your uploaded file must be of PDF. Other file types are not allowed<BR>";
			}
			else
			{
			
				$l_image_path           = "fpanel/images/uploads/".$insertStatus->id.".".$ext; // the path with the file name where the file will be stored,  
		
				if(move_uploaded_file ($_FILES['file']['tmp_name'],$l_image_path))
				{
				chmod("$l_image_path",0777);
				}
				else
				{
					echo "Failed to upload file Contact Site admin to fix the problem";
exit;
				}
			}
			/**********************image upload ******************************/
			switch($insertStatus->status)
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'form_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base').'upload_form/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base').'upload_form/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('form_add_failure');
					break;
			}
		} 
		$this->layout->adminview('form_addedit', $view);
	}
    function select_country()
	{		
	    $mylogin = $this->session->userdata('mylogin');
		$view['title']		      = 'Select Country';
		$view['subtitle']	      = 'Select Country';
		$view['menu_var']         = 3;
	    $where                    = array('deleted' => 0);
		$form                     = $this->form_model->getforms($page, $per_page_count , $like, $where );
		$view['form'] 		      = $form->records;
		$student                  = $this->student_model->getStudents(0,'','',array('users.id'=>$mylogin['id']));
		$view['student']          = $student->records[0];
		$save                     = $this->input->post('save');
	

		if(!empty($save)) 
		{
			$data['name']           = $mylogin['name'];
			$data['country_id']    	= $this->input->post('country_id');
			$sid        			= $mylogin['id'];
			$insertStatus           = $this->student_model->udpate($sid, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'country_edit_success');
					redirect($this->config->item('member_base').'upload_form/select_country/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
			}
			
		}
		$this->layout->memberview('form_select', $view);
	}
	function download_form($id='',$ext1='')
	{
		$p           = $this->config->item('member_image_path');
		$path = $p."uploads/"; // change the path to fit your websites document structure
        $fullPath = $_SERVER['DOCUMENT_ROOT'].$path.$id.".".$ext1;

        if ($fd = fopen ($fullPath, "r")) 
		{
           $fsize = filesize($fullPath);
    	   $path_parts = pathinfo($fullPath);
           $ext = strtolower($path_parts["extension"]);
    	   switch ($ext) {
        	case "pdf":
            header("Content-type: application/pdf"); // add here more headers for diff. extensions
            header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
            break;
            default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        }
	    header("Content-length: $fsize");
    	header("Cache-control: private"); //use this to open files directly
	    while(!feof($fd)) 
		{
	        $buffer = fread($fd, 2048);
        	echo $buffer;
    	}
	}
	fclose ($fd);
	exit;
	}
	function edit($id = '', $cur_page='')
	{
		$mylogin = $this->session->userdata('mylogin');
		$view                 = $this->getPath(__FUNCTION__);
		if(empty($id)) 
		{
			redirect($this->config->item('member_base').'form/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records = $this->student_model->getStudents(0,1,'', array('id' => $mylogin['id']));
			$student = $records->records[0];
			if(empty($form)) 
			{
				$this->session->set_flashdata('form_msg', 'form_invalid_record');
				redirect($this->config->item('admin_base').'form/index','location');
				exit;
			}
			$view['id']		                = $id;
			
			$view['form']	                = $form;
		}
		
		$view['title']                 = "Edit form ";
		$view['page_title']            = "Edit form";
		$view['subtitle']	           = 'Edit form';
		$save                          = $this->input->post('save');
		if(!empty($save)) 
		{
			$data                      = $this->retrieve->post($this->form_model->fields);
			$insertStatus              = $this->form_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('form_msg', 'form_edit_success');
					redirect($this->config->item('admin_base').'form/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('form_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('form_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('form_edit_failure');
					break;
			}
			$view['form'] 	       = $data;
		}
		$view['cur_page']              = $cur_page; 
		$this->layout->adminview('form_addedit', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base').'form/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'form/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->form_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'form_delete_success');
					redirect($this->config->item('admin_base').'form/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	    = $this->lang->line('form_delete_failure');
					break;
			}
		}
		
	}
	function clearFilter()
	{
		$this->session->unset_userdata('filter');
		redirect($this->config->item('admin_base').'form/index/','location');
	}
}
?>